/*
 * s_nor_flash.c
 *
 *  Created on: Sep 11, 2020
 *      Author: hemingway
 */
#include "s_nor_flash.h"

#include <stdlib.h>
#include <stdbool.h>

// #include "flash_interface.h"

#include <stdio.h>
#include <string.h>

// #include "h_cc2640r2f_define.h"

#include "h_stm32_hal_spi_norflash.h"

#define S_NOR_FLASH_START_ADDR				15728640 //15 * 1024 * 1024
#define S_NOR_FLASH_END_ADDR				16777216 //16 * 1024 * 1024


signed char S_Nor_Flash_Init(void)
{
    return 0;
}

signed char S_Nor_Flash_Write_Para_Struct(unsigned char *data, unsigned short len)
{
	unsigned char rslt = 0U;

	unsigned short clear_page_num = 1U, i = 0;

	if(len >= 4096)
	{
		clear_page_num = len / 4096;
		if(len % 4096)
		{
			clear_page_num++;
		}
	}

	// Not more than 16Mbit
	clear_page_num = clear_page_num > 255 ? 255 : clear_page_num;

	h_stm32_spi_norFlash_open();

	for(i = 0; i < clear_page_num; i++)
	{
		rslt = h_stm32_spi_norFlash_erasePage((S_NOR_FLASH_START_ADDR / 4096) + i);
	}

	if(rslt)
	{
		printf("eraese faied\r\n");
		h_stm32_spi_norFlash_close();
		return (-2);
	}

	rslt = h_stm32_spi_norFlash_write(S_NOR_FLASH_START_ADDR, data, len);

	h_stm32_spi_norFlash_close();

	if(rslt)
	{
		return (-1);
	}
	else
	{
		return 0;
	}
}

signed char S_Nor_Flash_Read_Para_Struct(unsigned char *data, unsigned short len)
{
	unsigned char rslt = 0U;

	h_stm32_spi_norFlash_open();

	rslt = h_stm32_spi_norFlash_read(S_NOR_FLASH_START_ADDR, data, len);

	h_stm32_spi_norFlash_close();

	if(rslt)
	{
		return (-1);
	}
	else
	{
		return 0;
	}
}


// #include "h_stm32_hal_uart.h"

#define EACH_DATA_LEN			128
// signed char S_Nor_Flash_Test_old(void)
// {
// 	h_stm32_spi_norFlash_open();

// 	unsigned char rdata[EACH_DATA_LEN];
// 	unsigned int i = 0;

// 	for(i = 0; i < EACH_DATA_LEN; i++)
// 	{
// 		rdata[i] = i;
// 	}


	
// 	// printf("eraese new state: 0x%02x\r\n", eraseFlashPgByAddr(S_NOR_FLASH_START_ADDR, EACH_DATA_LEN));
// 	printf("eraese state: 0x%02x\r\n", h_stm32_spi_norFlash_erasePage(0));

// 	printf("Write state: 0x%02x\r\n", h_stm32_spi_norFlash_write(0x00, rdata, EACH_DATA_LEN));

// 	// printf("eraese state: 0x%02x\r\n", H_STM32_SPI1_ErasePage(S_NOR_FLASH_START_ADDR / 4096));

// 	// printf("chip eraese state: 0x%02x\r\n", H_STM32_SPI1_EraseChip());

// 	memset(rdata, 0xFF, EACH_DATA_LEN);


// 	printf("read state %d\r\n", h_stm32_spi_norFlash_read(0x00, rdata, EACH_DATA_LEN));
// 	// printf("Read state: 0x%02x\r\n", H_STM32_SPI1_Read(0x00, rdata, EACH_DATA_LEN));
// 	// printf("Read state: 0x%02x\r\n", H_STM32_SPI1_ReadSectors(0, 1, rdata));

// 	debug_hex(rdata, EACH_DATA_LEN);
// 	printf("0x%02x\r\n", rdata[0]);

// 	// for(i = 0xe10000; i < 16777216; i+= EACH_DATA_LEN)
// 	// {
// 	// 	readFlash(i, rdata, EACH_DATA_LEN);
// 	// 	if(s_nor_flash_block_data_check(rdata, 0xFF, EACH_DATA_LEN))
// 	// 	{
// 	// 		printf("the i is 0x%x\r\n", i);
// 	// 	}
// 	// 	// else
// 	// 	// {
// 	// 	// 	printf(".");
// 	// 	// 	fflush(stdout);
// 	// 	// }
// 	// 	// eraseFlashPg(i);
		
// 	// }


	

// 	h_stm32_spi_norFlash_close();
// 	return 0;
// }

signed char S_Nor_Flash_Test(void)
{
	unsigned char rdata[EACH_DATA_LEN];
	unsigned int i = 0;

	for(i = 0; i < EACH_DATA_LEN; i++)
	{
		rdata[i] = i + 2;
	}

	S_Nor_Flash_Write_Para_Struct(rdata, EACH_DATA_LEN);

	memset(rdata, 0xFF, EACH_DATA_LEN);


	S_Nor_Flash_Read_Para_Struct(rdata, EACH_DATA_LEN);

	debug_hex(rdata, EACH_DATA_LEN);
}

